﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;

using OptusMobile.Web.ServerComponents.BAL.PlanManagement;
using OptusMobile.Web.ServerComponents.BAL.HandsetManagement;
using OptusMobile.Web.ServerComponents.DAL;
using OptusMobile.Web.ServerComponents.BAL.RecommenderManagement;
using OptusMobile.Web.ServerComponents.BAL.CustomerManagement;
using System.Drawing;


namespace OptusMobile.Web.Handset
{
    public partial class ShowHandsetDetail : System.Web.UI.Page
    {

        private PlanManager planManager;
        private HandsetManager handsetManager;
        private RecommenderManager recommenderManager = new RecommenderManager();
        private CustomerManager customerManager = new CustomerManager();

        public  ShowHandsetDetail()
        {
            planManager = new PlanManager();
            handsetManager = new HandsetManager();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            string cssPath = "../css/HandsetPlanDetail.css";

            HtmlLink link = new HtmlLink();
            link.Href = cssPath;
            link.Attributes["rel"] = "stylesheet";
            link.Attributes["type"] = "text/css";
            PlaceHolder1.Controls.Add(link);
            Session["detaileCurImage"] = 0;
            ShowHandsetImage(Request.QueryString["HandsetID"].ToString());
            ShowHandsetAllPlan(Request.QueryString["HandsetID"].ToString());
            ShowHandsetInformation(Request.QueryString["HandsetID"].ToString());
            
            if (!Page.IsPostBack)
            {
                DisplayRating();
            }
        }

        private void DisplayRating()
        {
            if (Session["ID"] != null)
            {
                string mobileNumber = Session["ID"].ToString();
                CustomerContract currentCustomer = customerManager.GetCustomerContractInformation(mobileNumber);
                string customerID = currentCustomer.Customer.customerID;
                int mark = this.Rating1.CurrentRating;
                string handsetID = Request.QueryString["HandsetID"].ToString();
                Rating rating = recommenderManager.GetRatingMark(customerID, handsetID);

                if (rating != null)
                {
                    this.Rating1.CurrentRating = rating.ratingMark;
                }
            }
        }

        protected void ShowHandsetImage(string HandsetID)
        {
            var img = handsetManager.GetHandsetImage().Single(p => p.handsetID == HandsetID);
            ImgHandset.ImageUrl = img.imagePath + img.imageName;
            Lbtn.Text = "See the Handset More Images";
            Lbtn.PostBackUrl = "ShowHandset.aspx?imageID="+img.imageID.ToString()+"&handsetID=" + HandsetID;
        }

        protected void ShowHandsetAllPlan(string HandsetID)
        {
            var plan = planManager.ReadAllCapDetails().Where(p => p.handsetID == HandsetID);

            
            foreach(V_CapDetail cap in plan)
            {
                var plandetail = planManager.ReadAllPlans().Single(m => m.planID == cap.planID);
                if (lbl19.ID.EndsWith(cap.planName.Substring(0,2)))
                {
                    lbl19.Text = "$"+plandetail.totalValue .ToString().Substring(0, plandetail.totalValue.ToString().IndexOf("."));
                    lblcr19.Text = plandetail.callRate.ToString().Substring(0, plandetail.callRate.ToString().IndexOf("."));
                    lbloo19.Text = plandetail.callRateToOptus.ToString().Substring(0,plandetail.callRateToOptus.ToString().IndexOf("."));
                    lblov19.Text = plandetail.callRateToOther.ToString().Substring(0,plandetail.callRateToOther.ToString().IndexOf("."));
                    lblsms19.Text = plandetail.perMessageFee.ToString().Substring(0, plandetail.perMessageFee.ToString().IndexOf(".")) + "C";
                    lblflag19.Text = plandetail.flagCall.ToString().Substring(0, plandetail.flagCall.ToString().IndexOf("."));
                    btn19.PostBackUrl = "BuyHandset.aspx?handsetID=" + Request.QueryString["HandsetID"].ToString();
                    btn19.Visible = true;
                }
                if (lbl49.ID.EndsWith(cap.planName.Substring(0, 2)))
                {
                    lbl49.Text = "$" + plandetail.totalValue.ToString().Substring(0, plandetail.totalValue.ToString().IndexOf("."));
                    lblcr49.Text = plandetail.callRate.ToString().Substring(0, plandetail.callRate.ToString().IndexOf("."));
                    lbloo49.Text = plandetail.callRateToOptus.ToString().Substring(0, plandetail.callRateToOptus.ToString().IndexOf("."));
                    lblov49.Text = plandetail.callRateToOther.ToString().Substring(0, plandetail.callRateToOther.ToString().IndexOf("."));
                    lblsms49.Text = plandetail.perMessageFee.ToString().Substring(0, plandetail.perMessageFee.ToString().IndexOf(".")) + "C";
                    lblflag49.Text = plandetail.flagCall.ToString().Substring(0, plandetail.flagCall.ToString().IndexOf("."));
                    btn49.PostBackUrl = "BuyHandset.aspx?handsetID=" + Request.QueryString["HandsetID"].ToString();
                    btn49.Visible = true;
                }
                if (lbl59.ID.EndsWith(cap.planName.Substring(0, 2)))
                {
                    lbl59.Text = "$" + plandetail.totalValue.ToString().Substring(0, plandetail.totalValue.ToString().IndexOf("."));
                    lblcr59.Text = plandetail.callRate.ToString().Substring(0, plandetail.callRate.ToString().IndexOf("."));
                    lbloo59.Text = plandetail.callRateToOptus.ToString().Substring(0, plandetail.callRateToOptus.ToString().IndexOf("."));
                    lblov59.Text = plandetail.callRateToOther.ToString().Substring(0, plandetail.callRateToOther.ToString().IndexOf("."));
                    lblsms59.Text = plandetail.perMessageFee.ToString().Substring(0, plandetail.perMessageFee.ToString().IndexOf(".")) + "C";
                    lblflag59.Text = plandetail.flagCall.ToString().Substring(0, plandetail.flagCall.ToString().IndexOf("."));
                    btn59.PostBackUrl = "BuyHandset.aspx?handsetID=" + Request.QueryString["HandsetID"].ToString();
                    btn59.Visible = true;

                }
                if (lbl79.ID.EndsWith(cap.planName.Substring(0, 2)))
                {
                    lbl79.Text = "$" + plandetail.totalValue.ToString().Substring(0, plandetail.totalValue.ToString().IndexOf("."));
                    lblcr79.Text = plandetail.callRate.ToString().Substring(0, plandetail.callRate.ToString().IndexOf("."));
                    lbloo79.Text = plandetail.callRateToOptus.ToString().Substring(0, plandetail.callRateToOptus.ToString().IndexOf("."));
                    lblov79.Text = plandetail.callRateToOther.ToString().Substring(0, plandetail.callRateToOther.ToString().IndexOf("."));
                    lblsms79.Text = plandetail.perMessageFee.ToString().Substring(0, plandetail.perMessageFee.ToString().IndexOf(".")) + "C";
                    lblflag79.Text = plandetail.flagCall.ToString().Substring(0, plandetail.flagCall.ToString().IndexOf("."));
                    btn79.PostBackUrl = "BuyHandset.aspx?handsetID=" + Request.QueryString["HandsetID"].ToString();
                    btn79.Visible = true;
                }
                if (lbl99.ID.EndsWith(cap.planName.Substring(0, 2)))
                {
                    lbl99.Text = "$" + plandetail.totalValue.ToString().Substring(0, plandetail.totalValue.ToString().IndexOf("."));
                    lblcr99.Text = plandetail.callRate.ToString().Substring(0, plandetail.callRate.ToString().IndexOf("."));
                    lbloo99.Text = plandetail.callRateToOptus.ToString().Substring(0, plandetail.callRateToOptus.ToString().IndexOf("."));
                    lblov99.Text = plandetail.callRateToOther.ToString().Substring(0, plandetail.callRateToOther.ToString().IndexOf("."));
                    lblsms99.Text = "UnLimit";
                    lblflag99.Text = plandetail.flagCall.ToString().Substring(0, plandetail.flagCall.ToString().IndexOf("."));
                    btn99.PostBackUrl = "BuyHandset.aspx?handsetID=" + Request.QueryString["HandsetID"].ToString();
                    btn99.Visible = true;
                }
            }


            
            
        }


        private void ShowHandsetInformation(string HandsetID)
        {
            var handset = handsetManager.GetAllHandsetDetailInformation().Single(p => p.handsetID == HandsetID);
            lblSize.Text = handset.handsetSize;
            lblScreenSize.Text = handset.handsetSreenSize;
            lblStandTime.Text = handset.handsetStandByTime;
            lblSystem.Text = handset.handsetSystem;
            lblWeight.Text = handset.handsetWeight;
            lblCon.Text = handset.handsetConnectivity;
            lblCamera.Text = handset.handsetCamera;
            lblBrowser.Text = handset.handsetBrowset;
        }

        protected void ButtonRate_Click(object sender, EventArgs e)
        {
            if (Session["ID"] != null)
            {
                string mobileNumber = Session["ID"].ToString();
                CustomerContract currentCustomer = customerManager.GetCustomerContractInformation(mobileNumber);
                string customerID = currentCustomer.Customer.customerID;
                int mark = this.Rating1.CurrentRating;
                string handsetID = Request.QueryString["HandsetID"].ToString();
                recommenderManager.RateHandset(customerID, handsetID, mark);

                this.LabelRate.Text = "Rate successfully!";
                this.LabelRate.ForeColor = Color.Green;
            }
            else
            {
                this.LabelRate.Text = "Please login for rate!";
                this.LabelRate.ForeColor = Color.Red;
            }
        }
    }
}
